package com.intellagric.service.agritech.impl;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.mapper.AgtResourceTypeMapper;
import com.intellagric.pojo.AgtResourceType;
import com.intellagric.pojo.AgtResourceTypeExample;
import com.intellagric.service.agritech.ResourceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: cwq
 * @Date: 2019/2/22 22:12
 * @Description:
 */
@Service
public class ResourceTypeServiceImpl implements ResourceTypeService {

    @Autowired
    private AgtResourceTypeMapper agtResourceTypeMapper;

    @Override
    public ResponseMessage addResourceType(AgtResourceType resourceType) {
        resourceType.setId(UUIDUtils.getID());
        resourceType.setCreatedTime(new Date());
        return agtResourceTypeMapper.insertSelective(resourceType) == 1 ? ResponseMessage.success().add("add", resourceType) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage deleteResourceType(String id) {
        return agtResourceTypeMapper.deleteByPrimaryKey(id) == 1 ? ResponseMessage.success() : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editResourceType(AgtResourceType resourceType) {
        return agtResourceTypeMapper.updateByPrimaryKeySelective(resourceType) == 1 ? ResponseMessage.success().add("update", resourceType) : ResponseMessage.fail();
    }

    @Override
    public AgtResourceType getResourceTypeById(String id) {
        return agtResourceTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getResourceTypeList() {
        AgtResourceTypeExample agtResourceTypeExample = new AgtResourceTypeExample();
        agtResourceTypeExample.setOrderByClause("created_time ASC");
        agtResourceTypeExample.createCriteria().andIdIsNotNull();
        List<AgtResourceType> agtResourceTypeList = agtResourceTypeMapper.selectByExample(agtResourceTypeExample);
        return LayuiDataGridResult.success().add(agtResourceTypeList, agtResourceTypeList.size());
    }

    @Override
    public LayuiDataGridResult getResourceTypeByPid(String pId) {
        AgtResourceTypeExample agtResourceTypeExample = new AgtResourceTypeExample();
        agtResourceTypeExample.setOrderByClause("created_time ASC");
        agtResourceTypeExample.createCriteria().andPidEqualTo(pId);
        List<AgtResourceType> agtResourceTypeList = agtResourceTypeMapper.selectByExample(agtResourceTypeExample);
        return LayuiDataGridResult.success().add(agtResourceTypeList, agtResourceTypeList.size());
    }

    @Override
    public LayuiDataGridResult getResourceTypeTree() {
        //获取所有分类
        List<AgtResourceType> agtResourceTypeList = this.getResourceTypeList().getData();

        //获取顶级分类
        List<AgtResourceType> rootAgtResourceTypeList = this.getResourceTypeByPid("0").getData();

        for (AgtResourceType agtResourceType : rootAgtResourceTypeList) {
            //每个分类项目都设置其子分类
            agtResourceType.setChildren(this.getChild(agtResourceType.getId(),agtResourceTypeList));
        }

        return LayuiDataGridResult.success().add(rootAgtResourceTypeList, rootAgtResourceTypeList.size());
    }

    @Override
    public LayuiDataGridResult getResourceTypeTreeByPid(String pid) {
        //获取所有分类
        List<AgtResourceType> agtResourceTypeList = this.getResourceTypeList().getData();

        //获取顶级分类
        List<AgtResourceType> rootAgtResourceTypeList = this.getResourceTypeByPid(pid).getData();

        for (AgtResourceType agtResourceType : rootAgtResourceTypeList) {
            //每个分类项目都设置其子分类
            agtResourceType.setChildren(this.getChild(agtResourceType.getId(),agtResourceTypeList));
        }

        return LayuiDataGridResult.success().add(rootAgtResourceTypeList, rootAgtResourceTypeList.size());
    }

    private List<AgtResourceType> getChild(String id, List<AgtResourceType> agtResourceTypeList) {
        //返回的子菜单
        List<AgtResourceType> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (AgtResourceType agtResourceType : agtResourceTypeList) {
            if (agtResourceType.getPid() != null) {
                //如果列表中的父id等于当前的分类的id，那就加入到该分类子列表中
                if (agtResourceType.getPid().equals(id)) {
                    childList.add(agtResourceType);
                }
            }
        }

        // 把子菜单的子菜单childList再循环一遍
        for (AgtResourceType agtResourceType : childList) {
            agtResourceType.setChildren(this.getChild(agtResourceType.getId(),agtResourceTypeList));
        }

        //退出递归的条件
        if (childList.size() == 0) {
            return null;
        }

        return childList;
    }
}
